package com.pet.server.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pet.server.domain.vo.ServeOrderVO;
import com.pet.server.service.IServeOrderService;
import com.pet.server.utils.ResultUtil;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ServeOrderServlet", urlPatterns = "/ServeOrderServlet")
public class ServeOrderServlet extends HttpServlet {
  @Resource IServeOrderService sos;

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    doGet(request, response);
  }

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    String method = request.getParameter("method");

    if (method == null || "".equals(method)) {
      getAllServeOrders(request, response);
    } else if (method.equalsIgnoreCase("getServeOrderInfos")) {
      getServeOrderById(request, response);
    } else if (method.equalsIgnoreCase("updateServeOrderSelective")) {
      updateServeOrderSelective(request, response);
    } else if (method.equalsIgnoreCase("queryByPhoneNum")) {
      findServeOrdersByPhoneNum(request, response);
    }
  }

  /** 通过电话号码查询订单 */
  private void findServeOrdersByPhoneNum(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    // 获取参数
    int pageNo = Integer.parseInt(request.getParameter("pageNo"));
    int pageSize = Integer.parseInt(request.getParameter("pageSize"));
    int shopId = Integer.parseInt(request.getParameter("shopId"));

    String phone = request.getParameter("phone");

    // 调用业务接口...
    List<ServeOrderVO> voList = sos.getServeOrdersByPhoneNum(pageNo, pageSize, shopId, phone);

    // 结果封装...
    ResultUtil<List> r = null;
    if (voList != null) {
      r = new ResultUtil<>(voList);
    } else {
      r = new ResultUtil<>("无数据");
    }

    response.getWriter().write(new ObjectMapper().writeValueAsString(r));
  }

  /** 更新订单数据 系统可供修改的数据: 服务规格,适用规格,服务等级,价格,排期 */
  private void updateServeOrderSelective(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    // 获取前端传入的json数据
    ServeOrderVO data = parseDataFromJson(request, response);

    // 调用业务接口
    boolean b = sos.updateServeOrderSelective(data);

    // 结果封装
    ResultUtil<Boolean> r = null;
    if (b) {
      r = new ResultUtil<>(b);
    } else {
      r = new ResultUtil<>("修改失败");
    }

    response.getWriter().write(new ObjectMapper().writeValueAsString(r));
  }

  /** 通过订单id查询订单详情 */
  private void getServeOrderById(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    // 获取门店id
    int shopId = Integer.parseInt(request.getParameter("shopId"));
    // 获取服务订单id
    int serveOrderId = Integer.parseInt(request.getParameter("serveOrderId"));

    // 查询....
    ServeOrderVO orderVO = sos.getServeOrderSelectiveById(serveOrderId, shopId);

    // 结果封装...
    ResultUtil<ServeOrderVO> r = null;
    if (orderVO != null) {
      r = new ResultUtil<>(orderVO);
    } else {
      r = new ResultUtil<>("无数据");
    }

    response.getWriter().write(new ObjectMapper().writeValueAsString(r));
  }

  /** 查询所有 */
  private void getAllServeOrders(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    // 获取参数..
    int pageNo = Integer.parseInt(request.getParameter("pageNo"));
    int pageSize = Integer.parseInt(request.getParameter("pageSize"));
    int shopId = Integer.parseInt(request.getParameter("shopId"));

    // 调用业务接口...
    List<ServeOrderVO> list = sos.getAllServeOrderList(pageNo, pageSize, shopId);

    // 结果数据封装..
    ResultUtil<List> r = null;
    if (list != null) {
      r = new ResultUtil<>(list);
    } else {
      r = new ResultUtil<>("无数据");
    }

    response.getWriter().write(new ObjectMapper().writeValueAsString(r));
  }

  /**
   * 通过Jackson的mapper方法 ,生成对象
   *
   * @param request req
   * @param response resp
   * @return 服务对象数据
   */
  private ServeOrderVO parseDataFromJson(HttpServletRequest request, HttpServletResponse response) {
    // 声明传入数据类型
    response.setContentType("application/json");

    ServeOrderVO vo = null;

    // 获取reader
    try (BufferedReader reader = request.getReader()) {
      // 读取json数据流
      String json = reader.readLine();

      System.out.println("json = " + json);
      // 通过Jackson的Mapper方法反序列化,生成对象

      vo = new ObjectMapper().readValue(json, ServeOrderVO.class);

    } catch (IOException e) {
      e.printStackTrace();
    }
    return vo;
  }
}
